
$(document).ready(function(){
    // render
    loadData();
    // event
    bindEventEdit();
});

function loadData() {
    var _maTV = ISS.SESSION.getSession('session_maTV');

    var account = {};
    account.thongTinThanhVien = _maTV;

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/editMemberAccount',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(account),
        success: function (data) {
            var responseContent = data.responseObject;
            loadDataComplete(responseContent);
        }
    });

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/getAllLevelInMember',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapAllLevelToView(responseContent);
        }
    });

}

function loadDataComplete(account){
    ISS.DOM.setVal('#AdMemberAccount_user', account.thongTinThanhVien.maTV);
    ISS.DOM.setVal('#AdMemberAccount_pass', account.matKhau);
    ISS.DOM.setVal('#AdMemberAccount_level_hidden', account.capBac.maCB);
}

function bindEventEdit(){
    ISS.EVENT.bindEvent('#RegeditForm_member_Modified1', 'click', function () {
        sendInfoEdit();
    });
}

function sendInfoEdit() {
    var account = {};
    account.matKhau = ISS.DOM.getVal('#AdMemberAccount_pass');

    var level = {};
    level.maCB = ISS.DOM.getVal('#AdMemberAccount_level');
    account.loaiDanhMuc = level;

    var code = {};
    code.maTV = ISS.DOM.getVal('#AdMemberAccount_user');
    account.thongTinThanhVien = code;

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/registerAccountEdit',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(account),
        success: function (data) {
            var responseContent = data.responseObject;
            sendInfoEditComplete(responseContent);
        }
    });
}

function sendInfoEditComplete(account) {
    if(account) {
        ISS.WINDOW.location('/jsp/admin/ad-member-account-common.jsp');
    }
    else{
        alert('Cập nhật không thành công');
    }
}

function mapAllLevelToView(account) {
    var level = ISS.DOM.getVal('#AdMemberAccount_level_hidden');
    var n = '';
    var html = '';
    for(var i=0; i<account.length; i++) {
        if (account[i].maCB == level){
            n = 'selected="selected"';
        }
        else{
            n = '';
        }
        html += '<option value="' + account[i].maCB + '" ' + n + '  >' + account[i].tenCB + '</option>';
    }
    ISS.DOM.html('#AdMemberAccount_level', html);
}
